<template>
  <div style="width: 100%; height: 100%">
    <div class="login">
      <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
      >
        <div class="login_left">
          <div class="left-title">
            <p class="title"></p>
            <p class="title">自助拉群平台</p>
          </div>
        </div>
        <div class="login_right">
          <div style="color: #000;font-size: 16px;text-align: right;margin-top: 60px">
            <el-dropdown trigger="click" class="international" @command="handleSetLanguage">
              <div style="font-size: 16px;color: #000;">
                <svg-icon class-name="international-icon" icon-class="language" />
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="zh">
                  简体中文
                </el-dropdown-item>
                <el-dropdown-item command="en">
                  English
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <!--登录状态切换-->
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                :placeholder="$t('login.username')"
                class="username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                :placeholder="$t('login.password')"
                class="password"
                @keyup.enter.native="handleLogin"
                show-password
            ></el-input>
          </el-form-item>
          <el-button :loading="loading"
                     type="primary"
                     style="width:100%;margin-bottom:30px;background-color: #e60012;"
                     @click.native.prevent="handleLogin">
            {{$t('login.logIn')}}
          </el-button>
        </div>
      </el-form>
      <!-- 技术支持部分 -->
      <!-- <div class="el-login-bottom">
        小白Rachel https://blog.csdn.net/Sabrina_cc
      </div> -->
    </div>
  </div>
</template>

<script>
import md5 from 'js-md5';

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error(this.$t('login.min6len')))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur',message: this.$t('login.usernameRequired')}],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      language:'zh',
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    handleSetLanguage(lang) {
      this.$i18n.locale = lang
      this.$store.dispatch('app/setLanguage', lang)
      this.$message({
        message: 'Switch Language Success',
        type: 'success'
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          let loginData = {
            username:this.loginForm.username,
            password:md5(this.loginForm.password)
          };
          this.loading = true
          this.$store.dispatch('user/login', loginData)
              .then(() => {
                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                this.loading = false
              })
              .catch((e) => {
                this.loading = false
                if(e.code != undefined && e.code == 1){
                  this.$notify.error({title:'登录失败',message:e.msg});
                }
              })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss"  scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;

  margin: 0 auto;
  width: 100%;
  height: 100%;
  /*width: 1920px;*/
  /*height: 1080px;*/
  background: url("../../assets/images/login/bg.png") center no-repeat;
  background-size: 100%;
}

.login-form {
  border-radius: 6px;
  background: url("../../assets/images/login/pic.png") left no-repeat;
  background-color: #fff;
  width: 800px;
  height: 500px;
  padding: 5px 25px 5px 25px;
  // margin-top: -50px;
  display: flex;

  .login_left {
    width: 300px;
    .left-title {
      margin: 60px 0px 0px 20px;
      width: 280px;
      height: 64px;
      font-size: 28px;
      font-family: Source Han Sans CN ExtraLight,
      Source Han Sans CN ExtraLight-ExtraLight;
      font-weight: ExtraLight;
      text-align: left;
      color: #ffffff;
      line-height: 40px;
      .title {
        margin: 5px;
        font-weight: 600;
      }
    }
  }
  /*右边的部分*/
  .login_right {
    width: 399px;
    margin-left: 60px;
    margin-top: 60px;
    padding: 0px 40px;
    img {
      border-radius: 4px;
      padding-bottom: 90px;
    }
    .right-title-content {
      height: 50px;
      width: 300px;
      display: flex;
      border-bottom: 1px solid #cccccc;
      .right-title {
        width: 150px;
        height: 50px;
        font-size: 24px;
        font-family: Source Han Sans CN Light, Source Han Sans CN Light-Light;
        font-weight: 400;
        text-align: center;
        color: #e60012;
        line-height: 40px;
        border-bottom: 2px solid #e60012;
      }
      .right-title-auto {
        width: 150px;
        height: 50px;
        font-size: 24px;
        font-family: Source Han Sans CN Light, Source Han Sans CN Light-Light;
        font-weight: 400;
        text-align: center;
        color: #999999;
        line-height: 40px;
        // border-bottom: 2px solid #e60012;
      }
    }
    .right-content {
      width: 300px;
      height: 350px;
      padding-top: 40px;
    }
  }
  /*输入框的间距*/
  .el-input {
    height: 42px;
    border: 1px solid #cccccc;
    // border-top: 0px;
    // border-left: 0px;
    // border-right: 0px;
  }
}
::v-deep .el-input__inner {
  font-size: 20px;
  background-color: rgba(255, 255, 255, 0);
  border: 0;
  height: 100%;
  width: 90%;
}

/*验证码*/
.login-code {
  width: 33%;
  height: 21px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  width: 105%;
  padding-bottom: 0px;
  // height:36px;
  /*height: 1px;*/
  /* background-color: #1e1e1e;*/
}

.login-form .login_right img {
  padding-bottom: 0px;
}
.el-login-header {
  height: 5px;
  position: fixed;
  /* bottom: 250px;*/
  top: 6%;
  right: 33%;
  width: 70%;
  text-align: center;
  img {
    width: 30%;
  }
}
.el-login-bottom {
  height: 20px;
  position: fixed;
  /* bottom: 250px;*/
  bottom: 8%;
  right: 0;
  left: 0;
  // width: 30%;
  text-align: center;
}

.username ::v-deep .el-input__inner {
  background: url("../../assets/images/login/user.png") no-repeat left;
  background-size: 8%;
  margin-left: 10px;
  // width:200px;
  padding: 0px 38px; /*输入内容左右边距控制*/
}
.password ::v-deep .el-input__inner {
  background: url("../../assets/images/login/pwd.png") no-repeat left;
  background-size: 8%;
  margin-left: 10px;
  padding: 0px 38px; /*输入内容左右边距控制*/
}
.code ::v-deep .el-input__inner {
  background: url("../../assets/images/login/yzm.png") no-repeat left;
  background-size: 10%;
  margin-left: 10px;
  padding: 0px 38px; /*输入内容左右边距控制*/
}
</style>
